'use strict';

const { Controller } = require('egg');
const { Op } = require('sequelize');
const serviceName = 'orderGoods'
function setWhere(query) {
  let {
    order_no = '',
    status,
    member_id,
    staff_id,
    start_time,
    serve_start_time,
    serve_end_time,
    end_time } = query;
  const where = {

  };
  if (member_id) {
    where.member_id = member_id;
  }
  if (staff_id) {
    where.staff_id = staff_id;
  }
  // 在时间范围内 包含当天0点到结束时间24点
  if (start_time && end_time) {
    where.create_time = {
      [Op.between]: [start_time, end_time]
    }
  }
  if (serve_start_time && serve_end_time) {
    where.serve_time = {
      [Op.between]: [serve_start_time, serve_end_time]
    }
  }
  /**
   * 订单状态 order_status -1 已取消 0 待接单 1 已接单 2服务中 3 服务完成
   * 支付状态 pay_status  0 待支付 1 支付成功 2 退款中 3退款成功
   * all 全部
   * pay 待支付
   * wait 待接单
   * get 已接单
   * serve 服务中
   * finish 服务完成
   * cancel 已取消
   */
  if (status === 'all') {

  }
  if (status === 'pay') {
    where.pay_status = 0
    where.order_status = 0
  }
  if (status === 'wait') {
    where.order_status = 0;
    where.pay_status = 1
  }
  if (status === 'get') {
    // order_status = 1 或者 10 为已接单
    where.order_status = { [Op.or]: [1, 2] };
    where.pay_status = 1
  }
  if (status === 'serve') {
    where.order_status = 3;
    where.pay_status = 1
  }
  if (status === 'finish') {
    where.order_status = 4;
    where.pay_status = 1
  }
  if (status === 'cancel') {
    where.order_status = -1;
    where.pay_status = 0
  }
  // 退款
    if (status === 'refund') {
    where.pay_status = 2
    }
  if (order_no) {
    where.order_no = { [Op.like]: `%${order_no}%` }
  }
  return where
}
class RouterController extends Controller {
  // 分页
  async index() {
    const { query, service } = this.ctx;
    let page = Number(query.pageNum) || 1;
    let size = Number(query.pageSize) || 10;

    const where = setWhere(query);
    let list = await service[serviceName].getPage(where, page, size);
    this.ctx.success(list);
  }
  // 新增
  async create() {
    const { ctx } = this;
    const data = ctx.request.body;
    const res = await ctx.service[serviceName].create(data);
    ctx.success(res, 200);
  }
  async refund() {
    const { ctx } = this;
    const { id } = ctx.request.body;
    if (!id) throw new Error('必须传递订单号');
    await ctx.service[serviceName].refund(ctx.request.body);
    ctx.success(id, 200);
  }
  async refundH5() {
    const { ctx } = this;
    const { id } = ctx.request.body;
    if (!id) throw new Error('必须传递订单号');
    await ctx.service[serviceName].refundH5(ctx.request.body);
    ctx.success(id, 200);
  }
  // 修改
  async update() {
    const { ctx } = this;
    const id = ctx.params.id;
    const infos = ctx.request.body;
    if (!id) throw new Error('必须传递ID参数');
    if (Object.keys(infos).length === 0) throw new Error('请传递修改的内容');
    await ctx.service[serviceName].update(id, infos);
    ctx.success(id, 200);
  }
  async updatePayStatus() {
    const { ctx } = this;
    const order_no = ctx.request.body.order_no;
    if (!order_no) throw new Error('必须传递订单号');
    await ctx.service[serviceName].updatePayStatus(order_no);
    ctx.success(order_no, 200);
  }
  // 删除
  async destroy() {
    const uid = Number(this.ctx.params.id);
    if (!uid) throw new Error('ID 有误');
    await this.ctx.service[serviceName].deleteById(uid);
    this.ctx.success(uid); // 删除成功返回被删除的用户ID
  }
  // 查看
  async show() {
    const { params, service } = this.ctx;
    const findItem = await service[serviceName].findOne(params.id);
    this.ctx.success(findItem);
  }
  // 所有列表
  async allList() {
    const { service } = this.ctx;
    const where = setWhere(this.ctx.query);
    const list = await service[serviceName].allList(where);
    this.ctx.success(list);
  }
}

module.exports = RouterController;
